﻿using SCADA_SANEST.Helpers;
using SCADA_SANEST.Models;
using SCADA_SANEST.Models.Line_1;
using SCADA_SANEST.ViewModels.Shares;
using SCADA_SANEST.Views.Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;


namespace SCADA_SANEST.ViewModels.Line_1
{
    public class Npc1ViewModel : DeviceViewModel
    {
        private Timer _updateTimer;
        public ICommand ShowPlcDetailCommand { get; }
        public PlcDeviceStatus Device { get; }
        public ObservableCollection<PlcSignal> PlcSignals { get; } = new ObservableCollection<PlcSignal>();

        public Npc1ViewModel(PlcDeviceStatus backend) : base(backend)
        {
            Device = backend;
            LoadPlcSignals();
            ShowPlcDetailCommand = new RelayCommand<object>(_ => true, _ => ShowPlcDetail());

            // Khởi động timer mỗi 1 giây để cập nhật giá trị
            _updateTimer = new Timer(UpdateSignalValues, null, 0, 1000);
        }

        private void ShowPlcDetail()
        {
            var detailViewModel = new PlcDataDetailViewModel(Device); // Device từ lớp cơ sở
            var window = new Window
            {
                Title = "Chi tiết PLC",
                Width = 600,
                Height = 400,
                Content = new PlcDataDetail { DataContext = detailViewModel }
            };
            window.ShowDialog();
        }

        private void LoadPlcSignals()
        {
            PlcSignals.Clear();
            foreach (var signal in Npc1SignalMap.GetSignals())
            {
                PlcSignals.Add(signal);
            }
        }

        private void StartAutoUpdate()
        {
            Task.Run(async () =>
            {
                while (true)
                {
                    Application.Current.Dispatcher.Invoke(UpdateSignalValues);
                    await Task.Delay(1000); // cập nhật mỗi giây
                }
            });
        }

        private void UpdateSignalValues(object state)
        {
            foreach (var signal in PlcSignals)
            {
                var value = Device.ReadValue(signal.Address, signal.Type);
                Application.Current.Dispatcher.Invoke(() =>
                {
                    signal.Value = value;
                });
            }
        }

        private string SimulateReadValue(PlcSignal signal)
        {
            switch (signal.Type)
            {
                case PlcDataType.Bool:
                    return (DateTime.Now.Second % 2 == 0).ToString(); // true / false
                case PlcDataType.Real:
                    return (25.5 + DateTime.Now.Second).ToString("F2"); // giả nhiệt độ
                case PlcDataType.Int:
                case PlcDataType.DInt:
                case PlcDataType.Word:
                    return DateTime.Now.Second.ToString();
                default:
                    return "?";
            }
        }
    }
}
